package com.example.constellation.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpUtils {
    public static String getjson(String path){
        String json="";
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        //将路径转换为url对象
        try {
            URL url=new URL(path);
            //获取网络连接对象
            HttpURLConnection conn= (HttpURLConnection) url.openConnection();
            //开始连接
            conn.connect();
            //读取输入流
            InputStream is = conn.getInputStream();//读取内容
            int hanread=0;
            byte[] buf=new byte[1024];
            //循环读取
            while (true){
                hanread=is.read(buf);
                if (hanread==-1) {
                    break;
                }
                baos.write(buf,0,hanread);
            }
            is.close();
            json=baos.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }
}
